package org.example.iocdemo;

import org.example.iocdemo.component.UserComponent;
import org.example.iocdemo.config.UserConfig;
import org.example.iocdemo.controller.UserContreller;
import org.example.iocdemo.model.UserInfo;
import org.example.iocdemo.repo.UserRepository;
import org.example.iocdemo.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class IoCDemoApplication {

    public static void main(String[] args) {
        //SpringApplication.run(IoCDemoApplication.class, args);
        //根据类型获取bean
        ApplicationContext context = SpringApplication.run(IoCDemoApplication.class,args);
        UserContreller userBean = context.getBean(UserContreller.class);
        userBean.getUser();
        //根据名称获取bean
        UserService userService = (UserService) context.getBean("userService");
        userService.sayHi();
        //根据名称和类型获取bean
        UserComponent userComponent = context.getBean("userComponent",UserComponent.class);
        userComponent.sayHi();
        //根据名称和类型获取bean
        UserRepository userRepository = context.getBean("userRepository", UserRepository.class);
        userRepository.sayHi();
        //根据名称和类型获取bean
        UserConfig userConfig = context.getBean("userConfig", UserConfig.class);
        userConfig.sayHi();

        UserInfo userInfo = context.getBean("u1",UserInfo.class);
        System.out.println(userInfo);

        UserInfo userInfo1 = context.getBean("u2", UserInfo.class);
        System.out.println(userInfo1);
    }

}
